package essential.ch3;

/**
 * @Date 2021/10/23
 * @Created by Jonathan
 */
public class StringLength2 {
    public static void main(String[] args) {
        int low = 0;
        int high = Integer.MAX_VALUE;
        int mid = (low + high) / 2;
        int max = low;
        while (low <= high) {
            for (int i = 0; i < 10; i++) {
                try {
                    char[] c = new char[mid];
                    max = mid;
                    low = mid + 1;
                    mid = (mid + high) / 2;
                    System.out.printf("%d-次申请成功：%d", (i + 1), max);
                    break;
                } catch (OutOfMemoryError e) {
                    if (i == 9) {
                        System.out.println("申请失败：" + mid);
                        high = mid - 1;
                        mid = (low + high) / 2;
                    }
                }
            }
        }
        System.out.println("可以上申请的最大值：" + max);
    }
}
